import React, { Component } from 'react'
import { connect } from 'react-redux'
// 引入actionCreator
import action from './store/actionCreator'
class Home extends Component {
  componentDidMount () {
    this.props.getBannerList()
    this.props.getProList({ count: 5, limitNum: 2 })
  }
  render() {
    const { bannerList, proList } = this.props
    return (
      <div>
        {
          bannerList && bannerList.map(item => (
            <img key = { item.bannerid } src={ item.img } alt={ item.alt }/>
          ))
        }
        {
          proList && proList.map(item => (
            <div key = { item.proid }>{ item.proname }</div>
          ))
        }
      </div>
    )
  }
}

export default connect(
  ({ bannerList, proList }) => ({ bannerList, proList }),
  (dispatch) => ({
    getBannerList() {
      // 调用使用 dispatch ， 原则都不加（）
      // 使用时 加不加（） 看有没有参数
      dispatch(action.getBannerListAction)
    },
    getProList (params) {
      // dispatch(action.getProListAction(params))
      fetch('http://121.89.205.189/api/pro/list')
        .then(res => res.json())
        .then(res => {
          console.log(res)
          dispatch({
            type: 'CHANGE_PRO_LIST',
            payload: res.data
          })
        })
    }
  })
  )(Home)
